package com.smc.hotfixdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.smc.hotfixdemo.utils.PermissionsUtil;
import com.smc.plugin.Plugin;

/**
 * 将class生成dex的命令
 * classDir 表示class文件所在的类名，包含包名路径
 * dx --dex --output out.dex classDir
 *
 */

public class MainActivity extends AppCompatActivity {

    private String[] PERMISSONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    private int request_code = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boolean bGranted = PermissionsUtil.checkPermissions(this, PERMISSONS);

        if (!bGranted){
            ActivityCompat.requestPermissions(this, PERMISSONS, request_code);
        }else {
            init();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (request_code == requestCode){
            boolean bGranted = PermissionsUtil.checkPermissions(this, PERMISSONS);
            if (bGranted){
                init();
            }else {
                Toast.makeText(this, "request permission faild", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void onWorkUIBtn(View view) {
        startLocalActivity(this, WorkActivity.class);
    }

    private void startLocalActivity(Context context, Class clazz){
        Intent localIntent = new Intent();
        localIntent.setClass(context, clazz);
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try{
            context.startActivity(localIntent);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private void init(){
//        String path = "/sdcard/plugin/1.apk";
//        Plugin.load(this, path);

    /*
        try {
            HookUtil.hookStartActivity_7(this);
            HookUtil.hookHandler(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    */

    }

    public void onPluginBtn(View view) {
//        Intent localIntent = new Intent();
//        localIntent.setClass(this, WorkActivity.class);
//        localIntent.putExtra(Def.KEY_IS_MAIN, true);
//        localIntent.putExtra(Def.KEY_PLUGIN_PACKAGE_NAME, "com.smc.hotfixdemo");
//        localIntent.putExtra(Def.KEY_PLUGIN_CLASS_NAME, "com.smc.loginmodule.MainActivity");
//        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        try{
//            this.startActivity(localIntent);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        String path = "/sdcard/plugin/1.dex";
        Plugin.load(this, path);

    }


}
